package com.schoolhelper.controller.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.schoolhelper.model.dao.okhttp.RefactorRequestUrl;
import com.schoolhelper.model.entity.User;
import com.schoolhelper.utils.MySymmetricEncryption;
import com.schoolhelper.utils.constant.Api;

import java.util.HashMap;

public class RegisterActivity extends AppCompatActivity {

    EditText edit_phone, edit_username, edit_pwd, edit_pwd_confirm;
    Button btn_return_login, btn_register;
    String username, pwd, pwd_confirm;
    String phone;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        edit_phone = findViewById(R.id.edit_phone);
        edit_username = findViewById(R.id.edit_username);
        edit_pwd = findViewById(R.id.edit_pwd);
        edit_pwd_confirm = findViewById(R.id.edit_pwd_confirm);
        btn_return_login = findViewById(R.id.btn_return_login);
        btn_register = findViewById(R.id.btn_register);

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = edit_username.getText().toString().trim();
                pwd = edit_pwd.getText().toString().trim();
                pwd_confirm = edit_pwd_confirm.getText().toString().trim();
//                phone = Integer.parseInt(edit_phone.getText().toString().trim());
                phone = edit_phone.getText().toString().trim();
                //判断输入是否为空，并判断密码与确认密码是否一致
                if ( phone.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请您输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                } else if (username.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请您输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                } else if (pwd.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请您输入密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (pwd_confirm.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请您确认密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(!pwd.equals(pwd_confirm)){
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致！", Toast.LENGTH_SHORT).show();
                    return;
                }
                pwd = MySymmetricEncryption.encrypt(pwd);
                pwd = MySymmetricEncryption.encrypt(pwd);
                User user = new User(null, username, pwd, null, null, phone, null, null, 100);
                System.out.println("AAA:"+user);
//                RequestUtil.post(Api.USER, user);
                System.out.println("USER:"+user);
//                JsonParser jsonParser = new EasyCodeJsonParser();
                String responseStr = RefactorRequestUrl.getPostResponseJson(Api.BASE_URL, Api.USER, new HashMap<>(), user);
                System.out.println("response:"+responseStr);
                Toast.makeText(RegisterActivity.this, responseStr, Toast.LENGTH_LONG).show();
                //                Intent intent = new Intent();
                //                intent.setClass(RegisterActivity.this, LoginActivity.class);
                //                startActivity(intent);
            }
        });

        btn_return_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent();
//                intent.setClass(RegisterActivity.this, LoginActivity.class);
//                startActivity(intent);
                finish();
            }
        });

        }
}
